package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerWriteInvitationCodeActivityComponent;
import com.wgts.wgts.dagger.module.WriteInvitationCodeActivityModule;
import com.wgts.wgts.mvp.presenter.IWriteInvitationCodeActivityPersenter;
import com.wgts.wgts.mvp.view.IWriteInvitationCodeActivityView;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.req.FireRequest;
import com.wgts.wgts.netmodle.resp.FireEntity;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WriteInvitationCodeActivity extends BaseActivity implements IWriteInvitationCodeActivityView {


    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.edit_invitation_code)
    EditText editInvitationCode;
    @BindView(R.id.tv_ensure)
    TextView tvEnsure;
    @BindView(R.id.linear_ensur)
    FrameLayout linearEnsur;
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;

    private String userPhone;

    @Inject
    IWriteInvitationCodeActivityPersenter persenter;


    @Override
    public void init() {
        DaggerWriteInvitationCodeActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .writeInvitationCodeActivityModule(new WriteInvitationCodeActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.send_invitation_code);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        userPhone = getIntent().getStringExtra("phone");
        tvPhone.setText(userPhone);
    }

    @Override
    public void fireResult(FireEntity fireEntity) {
        AppInfor.getInstance().setAccessToken(fireEntity.accessToken);
        AppInfor.getInstance().setRefreshToken(fireEntity.refreshToken);
        AppInfor.getInstance().setUserState(fireEntity.userRole);
        AppInfor.getInstance().setUserYQCode(fireEntity.userCode);
        AppInfor.getInstance().setInviteCode(fireEntity.userCode);

        Intent intent = new Intent(WriteInvitationCodeActivity.this, MoHuMainActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.linear_ensur, R.id.back_btn_image})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.linear_ensur:
                onClickEnsure();
                break;
            case R.id.back_btn_image:
                finish();
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }


    private void onClickEnsure(){

        if(editInvitationCode.getText().toString() != null && !editInvitationCode.getText().toString().equals("")){
            FireRequest request = new FireRequest();
            request.accessToken = AppInfor.getInstance().getAccessToken();
            request.userYQCode = editInvitationCode.getText().toString();
            persenter.fireUser(request);
        }else{
            MoHuToast.showToast("请填写邀请码");
        }

    }
}
